package com.aplink.generic.util.objectrule;

import java.util.ArrayList;

import com.aplink.generic.pojo.GenericAttribute;
import com.aplink.generic.pojo.GenericObject;
import com.aplink.generic.pojo.ObjPropInfo;

public class ActionCompareRule extends ActionBaseRule {
    private static final long serialVersionUID = -912325682704214751L;

    public ActionCompareRule() {
        this.arrObjPropInfo = new ArrayList<ObjPropInfo>();
    }

    public ActionCompareRule(final ArrayList<ObjPropInfo> _arrObjPropInfo) {
        this.arrObjPropInfo = new ArrayList<ObjPropInfo>();
        this.arrObjPropInfo = _arrObjPropInfo;
    }

    @Override
    public boolean isSatisfiedActionRule(final GenericObject currentObject) {

        int successCounter = 0;
        for (final ObjPropInfo objPropInfo : this.arrObjPropInfo) {
            // get Rule for this ObjPropInfo
            final ValueBaseRule valueBaseRule = objPropInfo.getValueBaseRule();

            // get Attribute of this ObjPropInfo from current GenericObject
            final GenericAttribute currentAttribute = currentObject
                    .get(objPropInfo.getColumnName());
            if (currentAttribute != null) {
                final boolean isSuccessful = valueBaseRule
                        .checkRule(currentAttribute);
                if (isSuccessful) {
                    successCounter++;
                }
            }
        }
        return successCounter == this.arrObjPropInfo.size();
    }

}
